<?php

namespace App\Listeners;

use App\Events\PlayerAddPollsEvent;
use App\Events\PlayerMinusPollsEvent;
use Exception;

class PlayerPollsListener
{
    /**
     * 增加票数的监听器，这么做，是为了有一个统一入口，为阶梯性的票数来统一处理
     *
     * @param  PlayerAddPollsEvent|PlayerMinusPollsEvent  $event
     * @return void
     * @throws Exception
     */
    public function handle(PlayerAddPollsEvent|PlayerMinusPollsEvent $event): void
    {
        $model = $event->model;

        if ($event instanceof PlayerAddPollsEvent) {
            $model->player()->increment('polls', $model->polls);
        } else {
            $model->player()->decrement('polls', $model->polls);
        }
    }
}
